package com.deli.dubbo.service;

import com.deli.cache.entity.Employee;
import com.deli.cache.mapper.EmployeeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Auther: xudeli
 * @Date: 2018/9/18/018 51
 * @Description:
 */
@Service
public class EmployeeService {

    private static final Logger log = LoggerFactory.getLogger(Employee.class);

    @Autowired
    EmployeeMapper employeeMapper;

    /**
     *  在方法上添加@Cacheable，将结果进行缓存再次调用不需要再次调用sql
     *  cacheNames，指定缓存组件的名字
     *  key，缓存使用的key。可以写#id或者#root.args[0],如果是一个参数，可以不写
     *  keyGenerator/key二选一
     *  cacheManager：制定缓存管理器
     *  cacheManager/cacheResolver二选一
     *  condition：指定符合条件的情况下才进行缓存
     *  unless :否定缓存，但unless指定条件下为true，方法的返回值不会被缓存
     *  sync:是否使用异步模式
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "emp",key = "#id",condition = "#id>3",unless = "#result == null")
    public Employee selectById(Integer id){
        log.info("into EmployeeService  method selectById param id : %s",id);
        return employeeMapper.selectById(id);
    }

    @CachePut(value = "updateEmp",key = "#result.id")
    public int updateEmp(Employee employee) {
        log.info("into EmployeeService method updateEmp param employee : %s",employee);
        return employeeMapper.updateEmp(employee);
    }
}
